const jwt = require("jsonwebtoken");
const { PRIVATE_KEY } = require("../config/screct");

class loginController {
  async sign(ctx, next) {
    // 1、派发token
    const { id, name } = ctx.user;
    const playload = { id, name };

    const token = jwt.sign(playload, PRIVATE_KEY, {
      expiresIn: 24 * 60 * 60,
      algorithm: "RS256",
    });

    ctx.body = {
      code: 1,
      data: {
        id,
        name,
        token,
      },
      message: "Login successful ~",
    };
  }

  test(ctx, next) {
    const result = ctx.user;
    ctx.body = {
      code: 1,
      result,
      message: "获取内容成功",
    };
  }
}

module.exports = new loginController();
